﻿using OneWorks.Metadata.Enum;

namespace OneWorks.Web.Common
{
    using System;
    using Application;
    using Application.Entity;
    using Application.Interface;
    using Metadata.Entity;
    using Model;

    public class ImageHandle
    {
        private readonly IImageService _imageService;
        private readonly IIconService _iconService;

        static ImageHandle()
        {
            AutoMapper.Mapper.CreateMap<Image, ImageModel>().ForMember(opt => opt.ImageID, otd => otd.MapFrom(e => e.ID));
            AutoMapper.Mapper.CreateMap<ImageModel, Image>().ForMember(opt => opt.ID, otd => otd.MapFrom(e => e.ImageID));
            AutoMapper.Mapper.CreateMap<FindImageModel, Image>().ForMember(opt => opt.ID, otd => otd.MapFrom(e => e.ImageID));
            AutoMapper.Mapper.CreateMap<Icon, IconModel>();
            AutoMapper.Mapper.CreateMap<IconModel, Icon>();
        }

        public ImageHandle()
        {
            _imageService = new ImageService();
            _iconService = new IconServer();
        }

        public RunResult<ImageModel> Add(ImageModel model)
        {
            Image image = AutoMapper.Mapper.Map<ImageModel, Image>(model);
            image.ID = Guid.NewGuid();
            image.CreateDate = DateTime.Now.Ticks;             
            RunResult<Image> runResult = _imageService.Add(image);
            model.ImageID = image.ID;
            return new RunResult<ImageModel>
            {
                Status = runResult.Status,
                Exception = runResult.Exception,
                Message = runResult.Message,
                Result = AutoMapper.Mapper.Map<Image, ImageModel>(runResult.Result)
            };
        }

        public RunResult<IconModel> Add(IconModel model)
        {
            Icon icon = AutoMapper.Mapper.Map<IconModel, Icon>(model);
            icon.IconID = Guid.NewGuid();
            icon.UserID = new UserContext().Current.UserCode;
            RunResult<Icon> runResult = _iconService.Add(icon);
            model.IconID = icon.IconID;
            return new RunResult<IconModel>
            {
                Status = runResult.Status,
                Exception = runResult.Exception,
                Message = runResult.Message,
                Result = AutoMapper.Mapper.Map<Icon, IconModel>(runResult.Result)
            };
        }

        public RunResult<PagedResult<ImageModel>> GetImages(FindImageModel model)
        {
            RunResult<PagedResult<ImageModel>> result = new RunResult<PagedResult<ImageModel>>
                                                            { Result = new PagedResult<ImageModel>() };
            Image image = AutoMapper.Mapper.Map<FindImageModel, Image>(model);
            RunResult<PagedResult<Image>> runResult = _imageService.Find(image, new FindPage
            {
                PageNumber = model.PageNumber,
                PageSize = model.PageSize
            });
            if (runResult.Status != ResultStatus.Success) return new RunResult<PagedResult<ImageModel>>
            {
                Status = runResult.Status,
                Exception = runResult.Exception,
                Message = runResult.Message,
                Result = new PagedResult<ImageModel>()
            };
            var page = runResult.Result;
            var list = page.GetEnumerator();
            while (list.MoveNext())
            {
                result.Result.Add(AutoMapper.Mapper.Map<Image, ImageModel>(list.Current));
            }
            result.Result.TotalRecords = page.TotalRecords;
            result.Result.TotalPages = page.TotalPages;
            return result;
        }
    }
}
